<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>人事管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../static/css/font.css">
    <link rel="stylesheet" href="../../static/css/xadmin.css">
    <script src="../../static/js/jquery-1.12.4.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../static/js/xadmin.js"></script>
</head>
<body class="layui-anim layui-anim-up">
<div class="x-body">
    <div class="layui-row demoTable">
        <form class="layui-form layui-col-md12 x-so">
            <div class="layui-input-inline">
                <select name="status" id='status'>
                    <option value="">选择状态</option>
                    <option value="1">未审批</option>
                    <option value="2">正常</option>
                </select>
            </div>
            <input type="text" name="username" id='username' placeholder="请输入登录名" autocomplete="off" class="layui-input">
            <button type="button" class="layui-btn" data-type="reload"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock class='demoTable'>
        <button class="layui-btn" onclick="x_admin_show('添加用户','./user-add.html',720,400)"><i
                class="layui-icon"></i>添加</button>
        <button  type="button" class="layui-btn layui-btn-danger" data-type="getCheckData"><i class="layui-icon layui-icon-delete"></i> 批量删除</button>
        <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon">ဂ</i>
        </a>
    </xblock>
    <table   class="layui-hide" id="user_table" lay-filter="demo"></table>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >
        <i class="layui-icon">&#xe642;</i>编辑
    </a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon">&#xe640;</i>删除
    </a>
</script>
<script>
    //**************数据报表格**********
    layui.use('table', function () {
        var table = layui.table;
        table.render({
            elem: '#user_table'
            , even: true  //隔行背景
            ,id:'user'
            , url: '../../userlist.action'
            , cols: [[
                {type:'checkbox'}
                ,{ field: 'id', title: 'ID', sort: true }
                , { field: 'loginname', title: '名字'}
                , { field: 'username', title: '昵称'}
                , { field: 'status', title: '性别',sort: true ,
                    templet: function(d){
                        if(d.status == 1){
                            return  `<span class="layui-badge">男</span>` }
                        else{return  `<span class="layui-badge layui-bg-blue">女</span>` }
                    }
                }
                , { field: 'createdate', title: '年龄', sort: true  }
                , { field: '', title: '电话', toolbar: '#barDemo' }
            ]]
            /* , page: true
            ,done: function (res, curr, count) {
                $('.layui-table').css("width","100%");
                $("th[data-field='bar']").css("border-right",'none');
              }
          ,limits:[5,10,15] */
        });
        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行id:'+data.id, function (index) {
                    $.ajax({
                        url: '../../delUser.action',
                        type: 'post',
                        data:{id:data.id},
                        success: function (data) {
                            if (data == 1) {
                                layer.close(index);
                                layer.msg('删除成功', {
                                    icon: 6,
                                    time: 1500
                                }, function(){
                                    location.replace(location.href);
                                });
                            }else if (data == 0){
                                layer.alert("删除失败！")
                            }else{
                                layer.msg('用户id:'+data+'正在线上，不能删除！',{icon:5,offset:"auto",time:2000});//提示框
                            }
                        },
                        error: function (e) {
                            layer.alert("error！提交失败！")
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                //layer.alert('编辑行：<br>' + JSON.stringify(data))
                localStorage.setItem("data",JSON.stringify(data)); //传参
                x_admin_show('编辑','user-edit.html',720,400)
            }
        });
        active = {
            reload: function(){
                var status = $('#status');
                var username = $('#username');
                //执行重载
                table.reload('user', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        status: status.val(),
                        username: username.val(),
                    }
                });
            },
            getCheckData:function(){ //获取选中数据
                var checkStatus = table.checkStatus('user')
                    ,data = checkStatus.data
                if(data==""){
                    layer.msg('至少也得选择一个',{icon:2});
                    return;
                }
                var ids=''
                if(data.length>0){
                    for (var i = 0; i < data.length; i++) {
                        ids += data[i].id;
                        if(i < data.length-1) ids += ",";
                    }
                }
                layer.confirm('确定要删除吗？'+ids,function(index){
                    $.ajax({
                        url:"../../delUsers.action",
                        data:{
                            "ids":ids
                        },
                        type:'post',
                        success:function(data){
                            if (data == 1) {
                                layer.msg('删除成功！',{icon:6,offset:"auto",time:2000});//提示框
                                setTimeout(function(){
                                    location.reload();//重新加载页面表格
                                }, 2100);
                            }else if (data == 0){
                                layer.msg('删除失败！',{icon:5,offset:"auto",time:2000});//提示框
                            }else{
                                layer.msg('用户id:'+data+'正在线上，不能删除！',{icon:5,offset:"auto",time:2000});//提示框
                            }
                        }
                    })
                })
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>